//
//  HROderViewController.m
//  LoveBee
//
//  Created by wyzc on 16/6/12.
//  Copyright © 2016年 叶落知秋. All rights reserved.
//
#define CATEGORYCELLID @"CATEGORY"
#import "HROderViewController.h"
#import <AFNetworking.h>
#import "HROrderCategoryModel.h"
#import "UPOrderCategoryCell.h"


@interface HROderViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *categoryTableView;
/**
 *  存放category的数据源
 */
@property(nonatomic,strong)NSMutableArray *categoryDataSource;

@end

@implementation HROderViewController
/**
 *  category懒加载
 */
-(NSMutableArray *)categoryDataSource
{
    if (_categoryDataSource==nil) {
        _categoryDataSource=[NSMutableArray array];
    }
    return _categoryDataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.barTintColor=HRCOLOR(255, 218, 0);
    //设置categorytableview的背景颜色
    self.categoryTableView.backgroundColor=HRCOLOR(248, 248, 248);
    
    [self setNavBar];
    [self setSearch];
    [self registerXib];
    [self sendRequest];
}

//注册Xib
-(void)registerXib
{
    UINib *categoryNib=[UINib nibWithNibName:@"UPOrderCategoryCell" bundle:nil];
    [self.categoryTableView registerNib:categoryNib forCellReuseIdentifier:CATEGORYCELLID];
}

//设置左侧扫描按钮
-(void)setNavBar
{
    UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [btn setBackgroundImage:[UIImage imageNamed:@"icon_black_scancode"] forState:UIControlStateNormal];
    UIBarButtonItem *item=[[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem=item;
    
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
}

//实现扫描按钮监听事件
-(void)click
{
    
}

//设置右侧搜索按钮
-(void)setSearch
{
    UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [btn setBackgroundImage:[UIImage imageNamed:@"icon_search"] forState:UIControlStateNormal];
    
    UIBarButtonItem *item=[[UIBarButtonItem alloc] initWithCustomView:btn];
    
    self.navigationItem.rightBarButtonItem=item;
    
    [btn addTarget:self action:@selector(searchClick) forControlEvents:UIControlEventTouchUpInside];
}

//实现搜索按钮监听事件
-(void)searchClick
{
    
}

//下载网络数据
-(void)sendRequest
{
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    [manager GET:@"http://m.beequick.cn/data/getCategoryProduct?asid=575d18dda79993326&_r=0.09407075636470152&tg=&tn=&reflogid=575d1fca7e9844954&zchtid=3248&bigids=2%2C0" parameters:nil success:^(NSURLSessionDataTask *task, NSDictionary *rootDict) {
        
        //把字典写入plist文件
        [rootDict writeToFile:@"/Users/wyzc/Desktop/FreshBee.plist" atomically:YES];
        NSDictionary *dataDict=rootDict[@"data"];
        NSArray *categories=dataDict[@"categories"];
        for (NSDictionary *dict in categories) {
            HROrderCategoryModel *model=[HROrderCategoryModel modelWithDict:dict];
            [self.categoryDataSource addObject:model];
            
            [self.categoryTableView reloadData];
        }

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}

#pragma mark--tableView数据源代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==self.categoryTableView) {
        return self.categoryDataSource.count;
}else
{
    return 0;
    
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==self.categoryTableView) {
        UPOrderCategoryCell *cell=[tableView dequeueReusableCellWithIdentifier:CATEGORYCELLID];
        HROrderCategoryModel *model=self.categoryDataSource[indexPath.row];
        cell.model=model;
        return cell;
    }else{
        return [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"a"];
    }
    
}

@end
